
package view.conponent;

import com.alibaba.fastjson.JSON;
import model.hospital.*;
import net.ToServer;
import net.passClass.PassBuildings;
import net.passClass.PassPatient;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.TreeSet;
import java.util.Vector;

public class BedManager extends JSplitPane {

    private final JTable ptable;
    private final Vector<Vector<String>> ptableData;
    private final DefaultTableModel ptableModel;

    private final JTree tree = new JTree();

    public BedManager() {

        requestData();

        JPanel treePanel = new JPanel();
        treePanel.setMaximumSize(new Dimension(220, 600));
        treePanel.setMinimumSize(new Dimension(220, 600));
        treePanel.setBackground(Color.WHITE);
        treePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        treePanel.add(tree);

        JScrollPane sp = new JScrollPane(treePanel);
        this.setLeftComponent(sp);
        this.setContinuousLayout(true);
        this.setDividerLocation(250);
        this.setDividerSize(8);

        Box vBox = Box.createVerticalBox();

        String[] ts = {"姓名","UID"};
        Vector<String> ptitles = new Vector<>(Arrays.asList(ts));

        ptableData = new Vector<>();

        ptableModel = new DefaultTableModel(ptableData, ptitles);
        ptable = new JTable(ptableModel){
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        ptable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);


        prequestData();

        JScrollPane patientList = new JScrollPane(ptable);
        JSplitPane getpatient = new JSplitPane();
        getpatient.setLeftComponent(vBox);
        getpatient.setRightComponent(patientList);
        getpatient.setDividerLocation(250);
        getpatient.setContinuousLayout(true);
        getpatient.setDividerSize(8);

        this.setRightComponent(getpatient);

        Box textFieldBox = Box.createVerticalBox();
        textFieldBox.setMaximumSize(new Dimension(300, 200));
        textFieldBox.setMinimumSize(new Dimension(200, 200));
        vBox.add(textFieldBox);

        Box btnBox = Box.createVerticalBox();
        btnBox.setMaximumSize(new Dimension(300, 200));
        btnBox.setMinimumSize(new Dimension(200, 200));
        vBox.add(btnBox);

        Box box1 = Box.createHorizontalBox();
        JTextField buildingNumber = new JTextField(9);
        JLabel buildingNumberStr = new  JLabel("楼号");
        box1.add(buildingNumberStr);
        box1.add(buildingNumber);
        textFieldBox.add(box1);

        Box box2 = Box.createHorizontalBox();
        JTextField floorNumber = new JTextField(9);
        JLabel floorNumberStr = new  JLabel("楼层");
        box2.add(floorNumberStr);
        box2.add(floorNumber);
        textFieldBox.add(Box.createVerticalStrut(10));
        textFieldBox.add(box2);

        Box box3 = Box.createHorizontalBox();
        JTextField roomNumber = new JTextField(9);
        JLabel roomNumberStr = new  JLabel("房号");
        box3.add(roomNumberStr);
        box3.add(roomNumber);
        textFieldBox.add(Box.createVerticalStrut(10));
        textFieldBox.add(box3);

        Box box4 = Box.createHorizontalBox();
        JTextField bedNumber = new JTextField(9);
        JLabel bedNumberStr = new  JLabel("床号");
        box4.add(bedNumberStr);
        box4.add(bedNumber);
        textFieldBox.add(Box.createVerticalStrut(10));
        textFieldBox.add(box4);

        Box box5 = Box.createHorizontalBox();
        JTextField patientUID = new JTextField(9);
        JLabel patientUIDStr = new  JLabel("病人UID");
        box5.add(patientUIDStr);
        box5.add(patientUID);
        textFieldBox.add(Box.createVerticalStrut(10));
        textFieldBox.add(box5);



        Box textFieldBox2 = Box.createVerticalBox();
        textFieldBox2.setMaximumSize(new Dimension(300, 200));
        textFieldBox2.setMinimumSize(new Dimension(200, 200));
        vBox.add(textFieldBox2);

        Box box12 = Box.createHorizontalBox();
        JTextField buildingNumber2 = new JTextField(9);
        JLabel buildingNumberStr2 = new  JLabel("楼号2");
        box12.add(buildingNumberStr2);
        box12.add(buildingNumber2);
        textFieldBox2.add(box12);

        Box box22 = Box.createHorizontalBox();
        JTextField floorNumber2 = new JTextField(9);
        JLabel floorNumberStr2 = new  JLabel("楼层2");
        box22.add(floorNumberStr2);
        box22.add(floorNumber2);
        textFieldBox2.add(Box.createVerticalStrut(10));
        textFieldBox2.add(box22);

        Box box32 = Box.createHorizontalBox();
        JTextField roomNumber2 = new JTextField(9);
        JLabel roomNumberStr2 = new  JLabel("房号2");
        box32.add(roomNumberStr2);
        box32.add(roomNumber2);
        textFieldBox2.add(Box.createVerticalStrut(10));
        textFieldBox2.add(box32);

        Box box42 = Box.createHorizontalBox();
        JTextField bedNumber2 = new JTextField(9);
        JLabel bedNumberStr2 = new  JLabel("床号2");
        box42.add(bedNumberStr2);
        box42.add(bedNumber2);
        textFieldBox2.add(Box.createVerticalStrut(10));
        textFieldBox2.add(box42);

        JButton check_inBtn = new JButton("签入");
        JButton check_outBtn = new JButton("签出");
        JButton exchangeBtn = new JButton("交换");

        btnBox.add(check_inBtn);
        btnBox.add(check_outBtn);
        btnBox.add(exchangeBtn);


        exchangeBtn.addActionListener(e -> {
            ToServer.receive("/hospital/bed/exchange",
                    "buildingNumber:" + buildingNumber.getText() + "\n" +
                            "floorNumber:" + floorNumber.getText() + "\n" +
                            "roomNumber:" + roomNumber.getText() + "\n" +
                            "bedNumber:" + bedNumber.getText() + "\n" +
                            "buildingNumber2:" + buildingNumber2.getText() + "\n" +
                            "floorNumber2:" + floorNumber2.getText() + "\n" +
                            "roomNumber2:" + roomNumber2.getText() + "\n" +
                            "bedNumber2:" + bedNumber2.getText());
            requestData();
        });

        check_outBtn.addActionListener(e -> {
            ToServer.receive("/hospital/bed/check_in",
                    "buildingNumber:" + buildingNumber.getText() + "\n" +
                            "floorNumber:" + floorNumber.getText() + "\n" +
                            "roomNumber:" + roomNumber.getText() + "\n" +
                            "bedNumber:" + bedNumber.getText());
            requestData();
        });

        check_inBtn.addActionListener(e -> {
            ToServer.receive("/hospital/bed/check_in",
                    "buildingNumber:" + buildingNumber.getText() + "\n" +
                            "floorNumber:" + floorNumber.getText() + "\n" +
                            "roomNumber:" + roomNumber.getText() + "\n" +
                            "bedNumber:" + bedNumber.getText() + "\n" +
                            "patientUID:" + patientUID.getText());
            requestData();
        });

        ptable.getSelectionModel().addListSelectionListener(e -> {
            int row = ptable.getSelectedRow();
            if(row != -1) {
                patientUID.setText(ptable.getValueAt(ptable.getSelectedRow(),1).toString());
            }
        });

    }

    public void requestData(TreeSet<Building> buildings){

        DefaultMutableTreeNode root = new DefaultMutableTreeNode("东软颐养社区中心"){
            {
                DefaultMutableTreeNode building;
                DefaultMutableTreeNode floor;
                DefaultMutableTreeNode room;
                DefaultMutableTreeNode bed;
                DefaultMutableTreeNode pat;

                for(Building aBuilding : buildings){
                    building = new DefaultMutableTreeNode("建筑"+aBuilding.getBuildingNumber());
                    for(Floor aFloor : aBuilding.getFloors()){
                        floor = new DefaultMutableTreeNode("楼层"+aFloor.getFloorNumber());
                        for(Room aRoom : aFloor.getRooms()){
                            room = new DefaultMutableTreeNode("房间"+aRoom.getLocation().getRoomNumber());
                            if(aRoom.getEquipment()==null){
                                for(Bed aBed : aRoom.getBeds()){
                                    bed = new DefaultMutableTreeNode("床位"+aBed.getBedNumber());
                                    if(aBed.getPatientUID()!=null){
                                        pat = new DefaultMutableTreeNode("病人:"+aBed.getPatientUID());
                                        bed.add(pat);
                                    }
                                    room.add(bed);
                                }
                            }
                            floor.add(room);
                        }
                        building.add(floor);
                    }
                    add(building);
                }

            }
        };
        tree.setModel(new DefaultTreeModel(root));
    }

    public void requestData(){

        PassBuildings data = JSON.parseObject(JSON.toJSONString(
                ToServer.receive("/hospital/get","\n")),PassBuildings.class);
        TreeSet<Building> buildings = data.getData();
        requestData(buildings);
    }

    public void prequestData(){
        ArrayList<Patient> data = ((PassPatient)ToServer.receive("/patient/get","\n")).getData();
        ptableData.clear();
        for(int i = 0; i<data.size(); i++){
            Patient patient = JSON.parseObject(JSON.toJSONString(data.get(i)),Patient.class);
            Vector<String> vector = new Vector<>();
            vector.add(patient.getName());
            vector.add(patient.getUid());
            ptableData.add(vector);
        }
        ptableModel.fireTableDataChanged();

    }

}